<h1>Compose a message</h1>
<p>
<a href="<?=Request::$controller?>">&larr; back to subscribers list</a>
</p>
<? if (count($subscribers)): ?>
<table class="w100">
	<tr>
		<td class="w33 paddingRight10">
			<table class="w100 TableWithPadding TableWithBorder" id="subscriberList">
				<thead>
					<tr>
						<th class="w5" filter="false">
							<input type="checkbox" id="checkAll"/>
						</th>
						<th class="w25">
							Name
						</th>
						<th class="w20" filter="false">
							Email
						</th>
					</tr>
				</thead>
				<tbody>
					<? foreach ($subscribers as $subscriber): ?>
						<tr <?=(($subscriber['confirmed']) ? '' : 'style="background-color: #eee; color: #999;"')?>>
							<td><input type="checkbox" class="check-row subscriber-<?=$subscriber['id']?>" id="subscriber-<?=$subscriber['id']?>" /></td>
							<td>
								<label for="subscriber-<?=$subscriber['id']?>">
									<?=Html::chars($subscriber['name'])?>
								</label>
							</td>
							<td><?=Html::chars($subscriber['email'])?></td>
						</tr>
					<? endforeach; ?>
				</tbody>
			</table>
		</td>
		<td class="w66">
			<?=(isset($form) ? $form : '')?>
			<br />
			<a href="#" id="send" class="awesome marginLeft10">Send message</a>
		</td>
	</tr>
</table>
<? else: ?>
	<h1>Add confirmed subscribers before composing a message.</h1>
<? endif; ?>
<script type="text/javascript">
	$(function() {
	    $('#checkAll').click(function () {
	        if ($(this).is(':checked')) {
	            $('.check-row').attr('checked', 'checked');
	        } else {
	            $('.check-row').removeAttr('checked');
	        }
	    });
	    $('#send').click(function (e) {
	        sid = $('.check-row:checked').map(function () {
	            var t;
	            if (t = this.className.match(/subscriber-([0-9]+)/)) {
	                return t[1];
	            } else {
	                return null;
	            }
	        }).get();
	        var sids = sid.join('|');
	        if (sids.length == 0) {
	        	alert('Please select at least one recepient');
	        } else {
	        	$('#message-recepients').val(sids);
	        	$('#message').submit();
	        }
	        return e.preventDefault();
	    } );
	});
</script>